package ac.neec.mio.record.search;

import ac.neec.mio.R;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;

/**
 * Created by G011C1093 on 14/01/25.
 */
public class SearchCalenderDialog extends DialogFragment {

	public static final int START_DATE_SETTING = 1;
	public static final int END_DATE_SETTING = 2;
	private static final String START_DATE_SETTING_TITLE = "開始日";
	private static final String END_DATE_SETTING_TITLE = "終了日";

	private Dialog dialog;
	private DatePicker datePicker;
	private CallbackListener activity;
	private CalendarView calendarView;
	private Button closeButton;
	private int isDate;
	private int nYear;
	private int nMonth;
	private int nDay;
	private boolean dayCangeFlag = false;
	private int insertYear;
	private int insertMonth;
	private int insertDay;

	public SearchCalenderDialog(int isDate, CallbackListener activity,
			int nYear, int nMonth, int nDay) {
		this.isDate = isDate;
		this.activity = activity;
		this.nYear = nYear;
		this.nMonth = nMonth;
		this.nDay = nDay;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		dialog = new Dialog(getActivity());
		dialog.setContentView(R.layout.dialog_setting_calender);
		initFindViews();
		setListener();
		setCalendar();
		setTitle();
		return dialog;
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		if (isDate == START_DATE_SETTING) {
			if (insertYear != 0)
				activity.startDateUpdate(insertYear, insertMonth, insertDay);

			else
				activity.startDateUpdate(nYear, nMonth, nDay);
		} else if (isDate == END_DATE_SETTING) {
			if (insertYear != 0)
				activity.endDateUpdate(insertYear, insertMonth, insertDay);
			else
				activity.endDateUpdate(nYear, nMonth, nDay);
		}
		if (!dayCangeFlag) {
			setSettingDate(nYear, nMonth, nDay);
		}
	}

	private void setTitle() {
		if (isDate == START_DATE_SETTING) {
			dialog.setTitle(START_DATE_SETTING_TITLE);
		} else if (isDate == END_DATE_SETTING) {
			dialog.setTitle(END_DATE_SETTING_TITLE);
		}
	}

	private void setCalendar() {
		datePicker.setCalendarViewShown(true);
		datePicker.setSpinnersShown(false);
		calendarView = datePicker.getCalendarView();
		calendarView.setShowWeekNumber(false);
		calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
			@Override
			public void onSelectedDayChange(CalendarView calendarView,
					int i, int i2, int i3) {
				dayCangeFlag = true;
				setSettingDate(i, i2 + 1, i3);
			}
		});
	}

	private void initFindViews() {
		datePicker = (DatePicker) dialog.findViewById(R.id.searchCalender);
		closeButton = (Button) dialog.findViewById(R.id.btnSetting);
	}

	private void setListener() {
		closeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				dismiss();
			}
		});
	}

	private void setSettingDate(int y, int m, int d) {
		this.insertYear = y;
		this.insertMonth = m;
		this.insertDay = d;
	}

}
